package com.cn.hnust.controller.hcontroller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.cn.hnust.pojo.Film;
import com.cn.hnust.service.IFilmService;

@Controller
@RequestMapping(value = "/hFilm")
public class HFilmController {

    @Autowired
    private IFilmService filmService;

    @RequestMapping("/toFilm")
    private String home(Model model, Film Film){
        List<Film> list=filmService.selectAllFilm();
        model.addAttribute("filmlist", list);
        return "film";
    }
     
    @RequestMapping("/addFilm")
    private String addEtl(@RequestParam(value="file") MultipartFile file,Model model, Film film,String mname,String taici,String area,String  mimg,String mdirector,String mactor,Integer mpf,String myear,String mbrief,String mifc) throws IllegalStateException, IOException{
  
    	 String filePath = "E:\\wangxeclipseworkspace\\ysss\\src\\main\\webapp\\filmimg";
         String originalFilename = file.getOriginalFilename();
         System.out.println("originalFilename:"+originalFilename);
         String newFileName = UUID.randomUUID() + originalFilename;
         File targetFile = new File(filePath, newFileName);
         file.transferTo(targetFile);
    	film.setMactor(mactor);
    	film.setMbrief(mbrief); 
    	film.setMdirector(mdirector); 
    	film.setMifc(mifc);
    	film.setMimg("http://localhost:8080/ysss/filmimg/"+newFileName);
    	film.setMname(mname);
    	film.setMpf(mpf);
    	film.setArea(area);
    	film.setMyear(myear);
    	film.setTaici(taici);
    	filmService.insertFilm(film);
        List<Film> list=filmService.selectAllFilm();
        model.addAttribute("etllist", list);
        return "film";
    }

    @RequestMapping("/toupdateFilm")
    private String toupdateFilm(Model model, Film film,Integer id){
    	System.out.println("修改文章："+id);
    	film=filmService.selectFilmById(id);
        model.addAttribute("film", film);
        return "updatefilm";
    }

    @RequestMapping("/updateFilm")
    private String updateFilm(Model model, Film film,Integer id,String mname,String  mimg,String taici,String area,String mdirector,String mactor,Integer mpf,String myear,String mbrief,String mifc){
     	film.setMactor(mactor);
    	film.setMbrief(mbrief);
    	film.setMdirector(mdirector);
    	film.setMifc(mifc);
    	film.setMname(mname);
    	film.setMpf(mpf);
    	film.setMyear(myear);
    	film.setArea(area);
    	film.setId(id);
    	film.setTaici(taici);
    	 List<Film> list=filmService.selectAllFilm();
         model.addAttribute("filmlist", list);
         return "film";
    }

    
    @RequestMapping("/deleteFilm")
    private String deleteFilm(Model model, Film Film,Integer id){
    
    	filmService.deleteFilm(id);
    	 List<Film> list=filmService.selectAllFilm();
         model.addAttribute("filmlist", list);
         return "film";
    }
}

